class IUP_LIST_DIALOG
-- Shows a modal dialog to select items from a simple or multiple selection 
-- list.
--
-- The dialog uses a global attribute called "PARENTDIALOG" as the parent 
-- dialog if it is defined. It also uses a global attribute called "ICON" as 
-- the dialog icon if it is defined.

inherit
	IUP_WIDGET

create {ANY}
   list_dialog_simple,
   list_dialog_multiple

feature {ANY}

	list_dialog_simple (title: STRING; list: ARRAY[STRING];
                       op, max_col, max_lin: INTEGER)
		-- Create a dialog for simple selection.
		--
		-- title: Text for the dialog’s title.
		-- list: List of options.
		-- op: Initial selected item. Starts at 1 (note that this index is 
		-- different from the return value, kept for compatibility reasons).
		-- max_col: number of visible columns in the list.
		-- max_lin: number of visible lines in the list.
		do
			t := 1
			ttl := title
			l := list
			o := op
			mc := max_col
			ml := max_lin

			convert_to_native(list)
		end

	list_dialog_multiple (title: STRING; list: ARRAY[STRING];
                         max_col, max_lin: INTEGER)
		-- Create a dialog for multiple selection.
		--
		-- title: Text for the dialog’s title.
		-- list: List of options.
		-- max_col: number of visible columns in the list.
		-- max_lin: number of visible lines in the list.
		do
			t := 2
			ttl := title
			l := list
			o := 0
			mc := max_col
			ml := max_lin

			convert_to_native(list)
		end

	launch: TUPLE[INTEGER, STRING]
			-- Show the dialog. If is a dialog for simple selection, the tuple 
			-- return an integer with the index of the selected item (starts 
	      -- at 0), or -1 if the user cancels the operation. Also return the 
			-- item itself, or an empty string if the user cancel the operation.
			-- If is a dialog of multiple selection, the function returns -1 when 
			-- the user cancels the operation and an empty string. If the user 
			-- does not cancel the operation the function returns 1 and the string 
			-- parameter will have value 1 for the options selected by the user 
			-- and value 0 for non-selected options (for example: "0010011100").
		local
			i, x: INTEGER
			so: STRING
			marks: NATIVE_ARRAY[INTEGER]
		do
			marks := marks.calloc(l.count)
			marks.set_all_with(0, l.count - 1)
			
			i := int_list_dialog (t, ttl.to_external,
			                      l.count, arg.to_external, o, mc, ml,
			                      marks.to_external)

			if i.is_equal(-1) then
				Result := [i, ""]
			else
				if t.is_equal(1) then
					Result := [i, l.item(l.lower + i)]
				else
					so := ""
					
					from
						x := 0
					until
						x.is_equal(l.count)
					loop
						so.append_string(marks.item(x).to_string)
						x := x + 1
					end
					
					Result := [i, so]
				end
			end
		end

feature {}

	t, o, mc, ml: INTEGER
	ttl: STRING
	l: ARRAY[STRING]

	arg: NATIVE_ARRAY[POINTER]

	-- Internals
	
	int_list_dialog (type: INTEGER; title: POINTER; size: INTEGER; list: POINTER; op, max_col, max_lin: INTEGER; marks: POINTER): INTEGER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupListDialog"
         }"
      end

	convert_to_native (col: ARRAY[STRING])
		local
			iterator: ITERATOR[STRING]; i: INTEGER; s: STRING
		do
			i := col.count
			arg := arg.calloc(i)
			iterator := col.new_iterator
			i := 0
			
			from
				iterator.start
         until
				iterator.is_off
         loop
            s := iterator.item
            arg.put(s.to_external, i)
				iterator.next
            i := i + 1
         end
		end

end -- class IUP_LIST_DIALOG

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
